package org.example.fronted.controller;

import org.example.entity.Data;
import org.example.fronted.service.ShowOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@CrossOrigin("*")
@Controller
public class SuccessController {

//    @Autowired
//    private PaymentController paymentController;

    @Autowired
    private ShowOrderService showOrderService;
//    public SuccessController(ShowOrderService showOrderService){
//        this.showOrderService=showOrderService;
//    }


    @RequestMapping("/checkPay")
    @ResponseBody
    public Object checkPay(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("showOrderService"+showOrderService);

        String oNo = (String) request.getSession().getAttribute("oNo");
        Data data = new Data();
        if (oNo != null) {
            boolean flag = showOrderService.checkPay(oNo);
            if (flag) {
                System.out.println("msg:" + "支付成功");
                data.setMsg("支付成功");
                data.setCode(1);
            }
        } else {
            data.setMsg("还没支付成功");
            data.setCode(-1);
        }

        return data;

    }

    @RequestMapping("/getImg")
    public void getImg(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("hello");
        Object img = request.getSession().getAttribute("imge");
        System.out.println("img" + img);
        try {
            ImageIO.write((RenderedImage) img, "png", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
